/*
 * @Author: your name
 * @Date: 2021-06-04 15:18:16
 * @LastEditTime: 2021-06-04 15:23:30
 * @LastEditors: your name
 * @Description: In User Settings Edit
 * @FilePath: \bazai_island_api\app\models\book.js
 */
const { Sequelize, Model } = require('sequelize')
const axios = require('axios')
const util = require('util')
const { sequelize } = require('@core/mysqlDB')

class Book extends Model {
  constructor (id) {
    super()
    this.id = id
  }
  // 获取详情
  async detail() {
    const url = util.format(global.config.yushu.detailUrl, this.id)
    const detail = await axios.get(url)
    return detail.data
  }
  // 获取我喜爱的图书数量
  static async getMyFavorBookCount(uid) {
    const count = await Favor.count({
      where: {
        type: 400,
        uid
      }
    })
    return count
  }
  // 搜索
  static async searchFromYuShu(q, start, count, summary=1) {
    const url = util.format(
      global.config.yushu.keywordUrl, encodeURI(q), count, start, summary
    )
    const detail = await axios.get(url)
    return detail.data
  }
}

Book.init({
  id: {
    type: Sequelize.INTEGER,
    primaryKey: true
  },
  fav_nums: {
    type: Sequelize.INTEGER,
    defaultValue: 0
  }
}, {
  sequelize,
  tableName: 'book'
})

module.exports = {
  Book
}
